package com.serverdemo.core.entity;

import com.serverdemo.core.net.NetPacket;
import io.netty.channel.Channel;

import java.util.LinkedList;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * 一个socket网络实例
 * Created by wangqiang on 2020/4/10.
 */
public class SocketEntity
{
    /**
     * 状态
     */
    private SocketEntityStatusEnum statusEnum = SocketEntityStatusEnum.NORMAL;

    /**
     * 已经接收到的消息包队列
     */
    private ConcurrentLinkedQueue<NetPacket> receivedPackets = new ConcurrentLinkedQueue<NetPacket>();

    /**
     * 待发送的消息包队列
     */
    private LinkedList<NetPacket> sendPackets = new LinkedList<NetPacket>();

    private Channel channel;

    public void pushReceivedPacket(NetPacket netPacket)
    {
        if (netPacket == null)
        {
            return;
        }

        receivedPackets.add(netPacket);
    }

    public NetPacket popReceivedPacket()
    {
        return receivedPackets.poll();
    }

    public void setStatusEnum(SocketEntityStatusEnum statusEnum)
    {
        this.statusEnum = statusEnum;
    }

    public SocketEntityStatusEnum getStatusEnum()
    {
        return statusEnum;
    }

    public NetPacket popSendPacket()
    {
        if (sendPackets.isEmpty())
        {
            return null;
        }
        return sendPackets.removeFirst();
    }

    public void pushSendPacket(NetPacket netPacket)
    {
        sendPackets.add(netPacket);
    }

    public Channel getChannel()
    {
        return channel;
    }

    public void setChannel(Channel channel)
    {
        this.channel = channel;
    }

}
